package server;





import iFace.WaveServerInterface;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;



public class WaveServer extends UnicastRemoteObject implements
WaveServerInterface {

	/**
	 *
	 */
	private static final long serialVersionUID = 5240687095466757710L;

	protected WaveServer() throws RemoteException {
	}

	public byte[][] getWavelet(byte[][] pic, int IMG_SIZE) throws RemoteException {

		byte[][] npic = new byte[IMG_SIZE][IMG_SIZE];

		// nivel 1
		for(int x=0; x<IMG_SIZE; x+=2) {
			for(int y=0; y<IMG_SIZE; y+=2) {
				npic[x/2][y/2] = (byte) ((pic[x][y]+pic[x+1][y]+pic[x][y+1]+pic[x+1][y+1])/4);
				npic[IMG_SIZE/2+x/2][y/2] = (byte) Math.abs(((pic[x][y]-pic[x+1][y]+pic[x][y+1]-pic[x+1][y+1])/4));
				npic[x/2][IMG_SIZE/2+y/2] = (byte) Math.abs(((pic[x][y]+pic[x+1][y]-pic[x][y+1]-pic[x+1][y+1])/4));
				npic[IMG_SIZE/2+x/2][IMG_SIZE/2+y/2] = (byte) Math.abs(((pic[x][y]-pic[x+1][y]-pic[x][y+1]+pic[x+1][y+1])/4));
			}
		}
		// nivel 2
		for(int x=0; x<IMG_SIZE/2; x++) {
			for(int y=0; y<IMG_SIZE/2; y++) {
				pic[x][y] = npic[x][y];
			}
		}
		for(int x=0; x<IMG_SIZE/2; x+=2) {
			for(int y=0; y<IMG_SIZE/2; y+=2) {
				npic[x/2][y/2] = (byte) ((pic[x][y]+pic[x+1][y]+pic[x][y+1]+pic[x+1][y+1])/4);
				npic[IMG_SIZE/4+x/2][y/2] = (byte) Math.abs(((pic[x][y]-pic[x+1][y]+pic[x][y+1]-pic[x+1][y+1])/4));
				npic[x/2][IMG_SIZE/4+y/2] = (byte) Math.abs(((pic[x][y]+pic[x+1][y]-pic[x][y+1]-pic[x+1][y+1])/4));
				npic[IMG_SIZE/4+x/2][IMG_SIZE/4+y/2] = (byte) Math.abs(((pic[x][y]-pic[x+1][y]-pic[x][y+1]+pic[x+1][y+1])/4));
			}
		}
		//		 nivel 3
		for(int x=0; x<IMG_SIZE/4; x++) {
			for(int y=0; y<IMG_SIZE/4; y++) {
				pic[x][y] = npic[x][y];
			}
		}
		for(int x=0; x<IMG_SIZE/4; x+=2) {
			for(int y=0; y<IMG_SIZE/4; y+=2) {
				npic[x/2][y/2] = (byte) ((pic[x][y]+pic[x+1][y]+pic[x][y+1]+pic[x+1][y+1])/4);
				npic[IMG_SIZE/8+x/2][y/2] = (byte) Math.abs(((pic[x][y]-pic[x+1][y]+pic[x][y+1]-pic[x+1][y+1])/4));
				npic[x/2][IMG_SIZE/8+y/2] = (byte) Math.abs(((pic[x][y]+pic[x+1][y]-pic[x][y+1]-pic[x+1][y+1])/4));
				npic[IMG_SIZE/8+x/2][IMG_SIZE/8+y/2] = (byte) Math.abs(((pic[x][y]-pic[x+1][y]-pic[x][y+1]+pic[x+1][y+1])/4));
			}
		}
		double tot;
		byte[][] apic = new byte[IMG_SIZE][IMG_SIZE];

		for(int x=0; x<IMG_SIZE; x++) {
			for(int y=0; y<IMG_SIZE; y++) {
				apic[x][y] = npic[x][y];
			}
		}
		for(int x=10; x<IMG_SIZE-10; x++) {
		    for(int y=10; y<IMG_SIZE-10; y++) {
			tot=0;
			for(int xi=-10; xi<10; xi++) {
			    for(int yi=-10; yi<10; yi++) {
				tot += npic[x+xi][y+yi];
			    }
			}
			tot = tot / (20*20);
			if (npic[x][y]<tot) apic[x][y]=0;
		    }
		}

		return(apic);
	}

	public static void main(String[] args) {
		System.setSecurityManager(new RMISecurityManager());
		if (args.length != 1) {
			System.err.println("Usage: server.WaveServer <server-rmi-url>");
			System.exit(-1);
		}
		try {
			String name = args[0];
			WaveServer server = new WaveServer();
			Naming.rebind(name, server);
			System.out.println("Started WaveServer, registered as " + name);
		} catch (Exception e) {
			System.out.println("Caught exception while registering: " + e);
			System.exit(-1);
		}
	}
}
